<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      :title="!dataForm.id ? $t('add') : $t('update')"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        class=""
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmitHandle()"
        :label-width="$i18n.locale === 'en-US' ? '110px' : '110px'"
      >
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="船名称" prop="boatName">
              <el-input v-model="dataForm.boatName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="船编号" prop="boatNo">
              <el-input v-model="dataForm.boatNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="船类型" prop="boatType">
              <el-select
                v-model="dataForm.boatType"
                placeholder="请选择船类型"
                clearable
                style="width:100%"
              >
                <el-option label="游玩船" :value="1"> </el-option>
                <el-option label="备用船" :value="2"> </el-option>
              </el-select>
              <!-- <ren-select
                v-model="dataForm.boatType"
                dict-type="MerchantType"
                placeholder="请选择商户类型"
                style="width: 100%"
              ></ren-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="载客数量" prop="capacity">
              <el-input v-model="dataForm.capacity"> </el-input>
            </el-form-item>
          </el-col>
           <!-- <el-col :span="24">
            <el-form-item label="船介绍" prop="info">
              <el-input v-model="dataForm.info" type="textarea" rows=4></el-input>
            </el-form-item>
          </el-col>
          <p class="pdl60">船只封面图<span>(建议尺寸220*256px)</span></p >
          <el-col :span="12">
            <el-form-item label="" prop="coverImg">
              <el-upload
                class="avatar-uploader"
                :action="url"
                :show-file-list="false"
                :on-success="
                  (res, file, fileList) => successHandle(res, file, fileList)
                "
                :before-upload="beforeUploadHandle"
              >
                <img
                  v-if="dataForm.coverImg"
                  :src="dataForm.coverImg"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template slot="footer">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
          $t("confirm")
        }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
import Cookies from "js-cookie";
export default {
  data() {
    return {
      mapShow: false,
      visible: false,
      url: `${
        window.SITE_CONFIG["apiURL"]
      }/oss/file/aliyunUpload?access_token=${Cookies.get("access_token")}`,
      fileList: [],
      dataForm: {
        boatName: "",
        boatNo: "",
        boatType: "",
        id: "",
        capacity: "",
        info:'',
        coverImg: "",
        merchantId: this.$store.state.user.MerchantId||localStorage.getItem('MerchantId'),
      },
    };
  },
  components: {},
  computed: {
    dataRule() {
      return {
        boatName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        boatType:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        capacity:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        info:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        coverImg:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
   
    // 上传之前
    beforeUploadHandle(file) {
      if (
        file.type !== "image/jpg" &&
        file.type !== "image/jpeg" &&
        file.type !== "image/png" &&
        file.type !== "image/gif"
      ) {
        this.$message.error(
          this.$t("upload.tip", {
            format: "jpg、png、gif",
          })
        );
        return false;
      }
    },
    // 上传成功
    successHandle(res, file, fileList) {
      if (res.code !== 0) {
        return this.$message.error(res.msg);
      }
      this.dataForm.coverImg = res.data.url;
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    //获取信息
    getInfo() {
      console.log(this.dataForm.id);
      this.$http
        .get("/scenic/boat/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/scenic/boat",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style lang="scss" scoped>
h3 {
  padding: 30px 0 15px;
  width: 94%;
  margin: 0 auto 30px;
  border-bottom: 1px solid #ddd;
  color: #303133;
  font-size: 22px;
  overflow: hidden;
}

.rooms .item {
  display: inline-block;
  margin-right: 15px;
  position: relative;

  .el-icon-error {
    position: absolute;
    right: 6px;
    top: -6px;
    font-size: 20px;
    color: #dd0606;
  }
}
</style>
